const list = require('../../global/addr');
const app = getApp();
const axios = require('../../utils/axios');

var page = 1;

Page({
  data: {
    margin: 0,
    height: 0,
    current_margin: 0,
    current_height: 0,
    currentDate: Date.now(),
    cascaderValue: '',
    list,
    actions: [
      { name: '全部类型', subname: '所有类型的招聘信息', value: null },
      { name: '义工招聘', subname: '招募志愿者、义工的招聘信息', value: 0 },
      { name: '实习招聘', subname: '企业实习生招聘信息', value: 1 },
      { name: '就业招聘', subname: '企业正式员工（含试用）招聘', value: 2 },
    ],

    show: {
      area: false,
      time: false,
      sort: false,
    },

    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      }
      if (type === 'month') {
        return `${value}月`;
      }
      if (type === 'day') {
        return `${value}日`;
      }
      return value;
    },
    date: null,
    area: null,

    data: [],
    total: 0,
    text: ''
  },

  onLoad(options) {
    page = 1;
    this.getHeaderStyle();
    this.getTabBar().setData({
      list: app.globalData.tabbars
    });
    this.getData();
  },

  getHeaderStyle() {
    const query = wx.createSelectorQuery();

    query.select('.header').boundingClientRect((rect) => {
      this.setData({ margin: rect.left, current_margin: rect.left });
    }).exec();
    query.select('.sub-back').boundingClientRect((rect) => {
      this.setData({ height: rect.top, current_height: rect.top });
    }).exec();
  },

  getData() {
    const { header } = app.globalData;
    axios.get('/staff', { page, size: 20, jName: this.data.text }, header).then(res => {
      const { code, data, count, err } = res;
      if(code == 200) {
        this.setData({ data: this.data.data.concat(data), total: count });
        page++;
      } else if (code == 401) {
        app.login(() => this.getData());
      } else throw err;
    }).catch(err => {
      wx.showModal({
        title: '错误',
        content: JSON.stringify(err),
        showCancel: false
      });
    });
  },

  onPageScroll(e) {
    const { margin, height } = this.data;
    const offset = e.scrollTop > height ? height : e.scrollTop;
    this.setData({
      current_height: height - offset,
      current_margin: margin * (height - offset) / height
    });
  },

  onShow() {
    if (typeof this.getTabBar === "function" && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 2,
      });
    }
  },
  
  onChangeShow(e) {
    const { key } = e.currentTarget.dataset;
    const name = 'show.' + key;
    this.setData({ [name]: !this.data.show[key] });
  },


  onChangeArea(e) {
    const list = e.detail.selectedOptions;
    console.log(list);
    this.setData({ cascaderValue: list[list.length - 1].value });
    list.forEach((item, index) => {
      item.value = item.value.slice(0, index * 2 + 2);
    });
    page = 1;
    this.getData();
    this.setData({
      area: list[list.length - 1].value,
      'show.area': false,
      data: []
    });
  },
  onChangeDate(e) {
    const time = timeFormat(e.detail, false);
    this.setData({
      date: time,
      'show.time': false,
      data: []
    });
    page = 1;
    this.getData();
  },

  onInput(e) {
    this.setData({ text: e.detail.value });
  },

  onJump(e) {
    const { url } = e.currentTarget.dataset;
    console.log(url);
    wx.navigateTo({
      url
    });
  },

  onReachBottom() {
    if(this.data.total == this.data.data.length) {
      return;
    } else {
      this.getData();
    }
  },
})